<?php

namespace Zilla\StoreBundle\Form\Handler;

use Symfony\Component\Form\Form;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\File\UploadedFile;
use Symfony\Component\HttpFoundation\File\File;
use Doctrine\ORM\EntityManager;
use Zilla\FrontBundle\Entity\Store;

class StoreFormHandler
{

    /**
     *
     * @var Request
     */
    protected $request;

    /**
     *
     * @var Form
     */
    protected $form;

    /**
     *
     * @var EntityManager
     */
    protected $em;

    /**
     *
     * @param Form $form
     * @param Request $request
     * @param EntityManager $em 
     */
    public function __construct(Form $form, Request $request, EntityManager $em)
    {
        $this->form = $form;
        $this->request = $request;
        $this->em = $em;
    }

    /**
     *
     * @param Store $store
     * @return bool
     */
    public function process(Store $store)
    {
        $this->form->setData($store);
        if ($this->request->getMethod() == 'POST') {
//            $files = $this->request->files->all();
//            if (empty($files['promotionandhistory']['promotion']['pictureName']) && $oldPictureName) {
//                $files['promotionandhistory']['promotion']['pictureName'] = new UploadedFile(__DIR__ . '/../../../../../web/uploads/promotions/' . $oldPictureName, $oldPictureName, mime_content_type(__DIR__ . '/../../../../../web/uploads/promotions/' . $oldPictureName));
//                $this->request->files->add($files);
//            }
            $this->form->bindRequest($this->request);
            if ($this->form->isValid()) {
//                $newPicture = $this->form['promotion']['pictureName']->getData();
//                if ($newPicture instanceof UploadedFile) {
//                    $promotionAndHistory->getPromotion()->setPictureName($this->uploadPicture($newPicture, $oldPictureName));
//                }
                $this->onSuccess($store);
                return true;
            }
        }
        return false;
    }

    /**
     *
     * @param Store $store 
     */
    protected function onSuccess(Store $store)
    {
//
//        $promotion = $promotionAndHistory->getPromotion();
//        $historyPromotion = $promotionAndHistory->getHistory();
//        if ($promotion->getActualState()) {
//            $historyPromotion->setStartingState($promotion->getActualState());
//        }
//
//        $historyPromotion->setReachedState($state);
//        $promotion->setActualState($state);
//
//
//        $historyPromotion->setPromotion($promotion);
        $store->setLastModifiedDate(new \DateTime());
        $this->em->persist($store);
        $this->em->flush();
    }

    protected function uploadPicture(UploadedFile $newPictureFile, $oldPictureName=null)
    {
        $extension = $newPictureFile->guessExtension();
        if (!$extension) {
            $extension = 'jpg';
        }
        $dir = __DIR__ . '/../../../../../web/uploads/promotions/';

        if ($newPictureFile->getClientOriginalName() != $oldPictureName) {
            if (file_exists($dir . $oldPictureName) && $oldPictureName) {
                unlink($dir . $oldPictureName);
            }
            $newPictureName = rand(1, 999999999) . '.' . $extension;
            while (file_exists($dir . $newPictureName)) {
                $newPictureName = rand(1, 999999999) . '.' . $extension;
            }
            $newPictureFile->move($dir, $newPictureName);
            return $newPictureName;
        } else {
            return $newPictureFile->getClientOriginalName();
        }
    }

}
